import org.junit.Test;

import java.util.Comparator;
import java.util.function.Consumer;
import java.util.logging.Logger;

/**
 * @author gzkemays
 * @date 2020/10/24 11:27
 */
public class jLambda {
    public static final Logger LOG =  Logger.getLogger(jLambda.class.getName());
    interface MathOperation {
        int operation(int a, int b);
    }
    private int operate(int a, int b, MathOperation mathOperation) {
        return mathOperation.operation(a, b);
    }
    MathOperation addition = (int a, int b) -> a + b;
    MathOperation multiplication = (int a, int b) -> a*b;

    @Test
    /**
     * lambda 优化
     */
    public void ComparatorTest() {
        /**
         * Java 7 调用匿名内部类
         */
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return 0;
            }

            @Override
            public boolean equals(Object obj) {
                return super.equals(obj);
            }
        };
        LOG.info("Java7(重写的):" + comparator.compare(1, 5));

        /**
         * Lambda 调用匿名内部类
         */
        Comparator<Integer> comparator1 = (a,b) -> Integer.compare(a,b);
        Comparator<Integer> comparator2 = Integer::compare;
        LOG.info("Java8:" + comparator2.compare(0,0));
    }

    @Test
    /**
     * 无参
     */
    public void OutputMessage() {
        /**
         * Java 7 执行
         */
        new Runnable() {
            @Override
            public void run() {
                System.out.println("执行Java7");
            }
        }.run();

        /**
         * Lambda
         */
        Runnable runnable = () ->  { System.out.println("Lambda"); };
        runnable.run();
        ( (Runnable) () -> System.out.println("Lambda") ).run();
    }

    @Test
    /**
     * 有参无返回值
     */
    public void OutputMessage2() {
        // old
        Consumer<String> consumer2 = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println("s = " + s);
            }
        };
        consumer2.accept("a");
        // lambda
        Consumer<String> consumer = (a) -> System.out.println("a = " + a);
        consumer.accept("执行输出 a");
    }

    @Test
    public void LambdaReduce() {
        jLambda jLambda = new jLambda();
        System.out.println(jLambda.operate(5,6,addition));
    }
}
